//+--------------------------------------------------------------------------------
//| Copyright (c) 2012 - 2013 ### an.vuongngocduy
//| This file is a part of Compact Game Engine (CGE)
//+--------------------------------------------------------------------------------
#include "cgeCanvasWin32.h"
#include "cgeEngine.h"

namespace cge
{
	//=========================================================================================
	CanvasWin32::CanvasWin32()
	{
		mWinHandle = 0;
	}

	//=========================================================================================
	CanvasWin32::~CanvasWin32()
	{
		
	}

	//=========================================================================================
	HWND CanvasWin32::GetHWND() const
	{
		return mWinHandle; 
	}

	//=========================================================================================
	void CanvasWin32::Setup(const String& title, const Vector2u& position, const Dimension2u& dimension, b8 fullScreen)
	{
		mRect.Set(position, dimension);

		HINSTANCE hInstance = GetModuleHandle(null);

		Dimension2u dim = mRect.Dimension();

		RECT rect = { 0, 0, dim.Width, dim.Height };

		AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, false, 0);

		WNDCLASSEX wndClass = 
		{
			sizeof(WNDCLASSEX),
			CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
			CanvasWin32::WindowProc,
			0, 0,
			hInstance,
			LoadIcon(null, IDI_APPLICATION),
			LoadCursor(null, IDC_ARROW),
			(HBRUSH)(COLOR_WINDOW + 1),
			null,
			TEXT("Compact Game Engine"),
			LoadIcon(null, IDI_APPLICATION)
		};

		if(!RegisterClassEx(&wndClass)) return;

		mWinHandle = CreateWindowEx
		(
			WS_EX_LEFT,
			TEXT("Compact Game Engine"), 
			TEXT(""),
			WS_OVERLAPPEDWINDOW,
			mRect.Left, mRect.Top,
			rect.right, rect.bottom,
			null, null, hInstance, null
		);

		if(!mWinHandle) return;

		SetWindowLong(mWinHandle, GWL_USERDATA, (LONG)this);
	}

	//=========================================================================================
	void CanvasWin32::Show()
	{
		UpdateWindow(mWinHandle);
		ShowWindow(mWinHandle, SW_SHOW);
	}

	//=========================================================================================
	void CanvasWin32::Render()
	{
		mRender->BeginScene();

		mRender->EndScene();
	}

	//=========================================================================================
	b8 CanvasWin32::WindowMessageDispatch(void* data)
	{
		MSG wndMsg;
		while(PeekMessage( &wndMsg, null, 0, 0, PM_REMOVE))
		{
			if(wndMsg.message == WM_QUIT)
				return true;

			TranslateMessage(&wndMsg);
			DispatchMessage(&wndMsg);
		}
		return false;
	}

	//=========================================================================================
	LRESULT CALLBACK CanvasWin32::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		CanvasWin32* cw = (CanvasWin32*)GetWindowLong(hWnd, GWL_USERDATA);
	
		switch(uMsg)
		{
		case WM_CLOSE:
			PostQuitMessage(0);
			return 0;
			
		case WM_SIZE:
			return 0;

		case WM_MOVE:
			return 0;

		case WM_PAINT:
			break;
		}
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}

	//=========================================================================================

}